//
//  VIZVistorLoginView.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/7.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VIZVistorLoginViewDelegate : NSObjectProtocol {
    
    func userWillLogin()
    
    func userWillReisgin()
    
    
}



class VIZVistorLoginView: UIView {
    
    
    //设置遵守协议的代理
    weak var delegate : VIZVistorLoginViewDelegate?
    
    //大房子
    lazy var house:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆环
    lazy var circle:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //背景图
    private lazy var backGround:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示消息
//    lazy var label:UILabel = {
//        let l = UILabel()
//        l.text = "关注一些人,回这里看看有什么惊喜"
//        l.font = UIFont.systemFontOfSize(14)
//        l.textColor = UIColor.lightGrayColor()
//        l.sizeToFit()
//        l.numberOfLines = 0
//        l.textAlignment = .Center
//        return l
//    }()
    lazy var label:UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", textColor: UIColor.lightGrayColor(), fontSize: 14)
    
    //登录按钮
    lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        //设置图片
        let image = UIImage(named: "common_button_white_disable")
        let capW = Int((image?.size.width)!*0.5)
        let capH = Int((image?.size.height)!*0.5)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return btn
    }()
    
    //注册按钮
    lazy var reisterBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
    
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        self.backgroundColor = UIColor.redColor()
        //调用布局方法
        self.setFrame()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setFrame(){
        
        self.addSubview(circle)
        self.addSubview(house)
        self.addSubview(backGround)
        self.addSubview(label)
        self.addSubview(loginBtn)
        self.addSubview(reisterBtn)
        
        //设置背景色  使用sRGB 取色  不过在不同的屏幕上显示的颜色是不一样的
        let value: CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //圆环
        circle.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        //背景图
        backGround.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }

        //大房子
        house.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            bringSubviewToFront(house)
        }

        //label
        label.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circle.snp_centerX)
            make.centerY.equalTo(circle.snp_bottom).offset(16)
            make.height.equalTo(40)
            make.width.equalTo(230)
            
        }
        
        //loginBtn
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(label.snp_bottom).offset(16)
            make.left.equalTo(label.snp_left)
            make.width.equalTo(100)
            
        }

        //注册按钮
        reisterBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(label.snp_bottom).offset(16)
            make.right.equalTo(label.snp_right)
            make.width.equalTo(100)
            
        }
        
        
        //按钮添加点击方法
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        reisterBtn.addTarget(self, action: "reisterBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    func loginBtnClick(){
        
        //print("登录")
        
        delegate?.userWillLogin()
        
    }
    
    func reisterBtnClick() {
        
        //print("注册")
        delegate?.userWillReisgin()
        
    }
    
    

    func startAnimation() {
        
        let anm = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anm.repeatCount = MAXFLOAT
        //持续时间
        anm.duration = 20
        //旋转角度
        anm.toValue = 2*M_PI
        // 动画执行结束 或者 当前处于非活跃状态下 动画不移除图层
        anm.removedOnCompletion = false
        //加到圆圈图层
        circle.layer.addAnimation(anm, forKey: nil)
        
    }
    
    //外界调用的方法显示游客登录
    func setVistorLogin(imageName:String, labelText:String, isHome:Bool){
        
        if isHome {
            self.startAnimation()
        }else{
            house.hidden = true
            bringSubviewToFront(circle)
        }
        circle.image = UIImage(named: imageName)
        label.text = labelText

    }
    
    
    
    
    
    
    
    
    
    

}
